package ganga.custodian.notification;

import java.io.FileNotFoundException;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.telephony.SmsManager;

import ganga.custodian.HeartBeatService;
import ganga.custodian.R;

public class NotificationExecutor {
	public NotificationExecutor(Context context) {
		super();
		this.context = context;
		humanLocation = new Location("");
		//Дизайн-завод "Флакон" 55.354135,40.297852
		humanLocation.setLatitude(55.354135);
		humanLocation.setLongitude(40.297852);
		
		//НТК 55.736268,37.62208
		humanLocation.setLatitude( 55.736268);
		humanLocation.setLongitude(37.62208);

	}
	private static final String Apocrypha = "tel:89035727126";
	private static final String GI = "tel:89629515137";//Геннадий Иванович, личный
	private static final String CustodianNumber = "tel:89651163787";//Геннадий Иванович, НТК-служебный
	private static final String NUMBER_TO_CALL = GI;
	private static final String NUMBER_TO_SMS  = GI;
	private Location humanLocation;

	Context context;

	public void call(){
		try{
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse(NUMBER_TO_CALL));
			//Calling startActivity() from outside of an Activity context 
			//requires the FLAG_ACTIVITY_NEW_TASK flag.
			//See http://stackoverflow.com/questions/5308136/error-while-calling-service-from-activity
			callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(callIntent);
		}
		catch (ActivityNotFoundException ex){
			ex.printStackTrace();
		}
	} 
	public void sendSms(){
		SmsManager smsManager = SmsManager.getDefault();
		String smsText = String.format("%s Координаты: Широта: %f Долгота: %f", 
				context.getString(R.string.AlarmButtonSmsMessage), 
				humanLocation.getLatitude(), 
				humanLocation.getLongitude());
		smsManager.sendTextMessage(NUMBER_TO_SMS, null, smsText , null, null);
	}
	private void play(String fileName) throws FileNotFoundException{
		java.io.File file = new java.io.File(fileName);
		//if (!file.exists()) 
			//throw new FileNotFoundException(String.format("Отсутствует звуковой файл %s", fileName));
		
		MediaPlayer player = MediaPlayer.create(context, Uri.parse(fileName));
//		try {
//			//player.prepare();
//			;
//		} catch (IllegalStateException e) {
//			e.printStackTrace();
//			player.reset();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		try {
			player.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void doPlayTaskManFillBad(){
		new ManFillBadPlayerTask().execute();
	}
	public void doPlayTaskManHROverhead(){
		new HROverheadPlayerTask().execute();
	}
	public class ManFillBadPlayerTask extends AsyncTask <Void,Void,Void>{

		@Override
		protected Void doInBackground(Void... arg0) {
			try {
				play("file:///sdcard/HumanFillBad.mp3");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			return null;
		}		
	} //Task
	public class HROverheadPlayerTask extends AsyncTask <Void,Void,Void>{

		@Override
		protected Void doInBackground(Void... arg0) {
			try {
				play("file:///sdcard/HROverHead.mp3");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			return null;
		}		
	} //Task

}